package com.sgb.member.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityClientPropertyBinding;
import com.sgb.member.di.DaggerMemberComponent;
import com.sgb.member.di.MemberModule;
import com.sgb.member.ui.fragment.FixedAssetsFragment;
import com.sgb.member.ui.fragment.IntellectualPropertyFragment;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.MToast;

import java.util.ArrayList;

/**
 * Created by：冯齐 on 2021/1/26 16:42
 * Description：会员资产
 */
public class ClientPropertyActivity extends AppActivity {

    private ActivityClientPropertyBinding mBinding;
    private final String[] mTitles = {
            "商标产权", "版权产权", "专利产权", "专项产权"
    };
    private ArrayList<Fragment> mFragments;

    public static void start(Context context) {
        Intent intent = new Intent(context, ClientPropertyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMemberComponent.builder()
                .appComponent(appComponent)
                .memberModule(new MemberModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_client_property);
        initSearchView();
        mFragments = new ArrayList<>();
        mFragments.add(IntellectualPropertyFragment.newInstance(0));
        mFragments.add(IntellectualPropertyFragment.newInstance(1));
        mFragments.add(IntellectualPropertyFragment.newInstance(2));
        mFragments.add(FixedAssetsFragment.newInstance());
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mBinding.vp.setOffscreenPageLimit(mTitles.length);
        mBinding.vp.setAdapter(mPagerAdapter);
        mBinding.tabLayout.setupWithViewPager(mBinding.vp);
        mBinding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mBinding.tabLayout.setNormalColor(R.color.color_main_bg);
        mBinding.tabLayout.setSelectColor(R.color.color_white);
        mBinding.tabLayout.setNormalSize(16);
        mBinding.tabLayout.setSelectSize(18);
        mBinding.tabLayout.setTabMode(TabLayout.MODE_FIXED);
        mBinding.tabLayout.addOnTabSelectedListener(mTitles, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String hint = mHintArray.get(tab.getPosition());
                mBinding.activityBar.etView.setHint(hint);
                mBinding.vp.setCurrentItem(tab.getPosition());
            }
        });
    }

    private String mSearchText = ""; // 搜索内容
    private final SparseArray<String> mHintArray = new SparseArray<>();

    private void initSearchView() {
        mHintArray.put(0, "请输入商标名称");
        mHintArray.put(1, "请输入版权名称");
        mHintArray.put(2, "请输入专利名称");
        mHintArray.put(3, "请输入公司名称");
        mBinding.activityBar.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mBinding.activityBar.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mBinding.activityBar.etView.setHint(mHintArray.get(0));
        mBinding.activityBar.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), mBinding.activityBar.etView.getHint().toString());
                        return true;
                    }
                    if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), "请输入不同的搜索内容");
                        return true;
                    }
                    mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                    loadSearchData();
                }
                return false;
            }
        });
        mBinding.activityBar.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s) || s.length() == 0) {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
                } else {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBinding.activityBar.tvSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchText = "";
                mBinding.activityBar.etView.setText("");
                loadSearchData();
            }
        });
        mBinding.activityBar.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mSearchText)) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), mBinding.activityBar.etView.getHint().toString());
                        return;
                    }
                }
                if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                    MToast.showToast(getActivity(), "请输入不同的搜索内容");
                    return;
                }
                mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                loadSearchData();
            }
        });
        mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
    }

    public String getSearchText() {
        return mSearchText;
    }

    private void loadSearchData() {
        Fragment fragment = mFragments.get(mBinding.vp.getCurrentItem());
        if (fragment == null) return;
        if (fragment instanceof FixedAssetsFragment) {
            ((FixedAssetsFragment) fragment).loadData(false);
        } else if (fragment instanceof IntellectualPropertyFragment) {
            ((IntellectualPropertyFragment) fragment).loadData(false);
        }
    }
}
